package Client;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import comunicationFrames.LoginResponse;


@SuppressWarnings("serial")
public class LoginFrame extends JFrame {

	private JPanel contentPane;
	private JTextField loginField;
	private JPasswordField passwordField;
	private ComunicatorClient client;

	public LoginFrame(ComunicatorClient client) {
		this.client = client;
		
		setResizable(false);
		setTitle("Logowanie");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocation(new Point(200,200));
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		contentPane.add(panel);
		panel.setLayout(new GridLayout(2, 1, 0, 0));
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new EmptyBorder(3, 3, 3, 3));
		panel.add(panel_1);
		
		JLabel lblLogin = new JLabel("Login: ");
		panel_1.add(lblLogin);
		
		loginField = new JTextField();
		panel_1.add(loginField);
		loginField.setColumns(12);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new EmptyBorder(3, 3, 3, 3));
		panel.add(panel_2);
		
		JLabel lblNewLabel = new JLabel("Has\u0142o: ");
		panel_2.add(lblNewLabel);
		
		passwordField = new JPasswordField();
		panel_2.add(passwordField);
		passwordField.setColumns(12);
		
		JPanel panel_3 = new JPanel();
		contentPane.add(panel_3, BorderLayout.SOUTH);
		
		JButton btnNewButton = new JButton("Zaloguj");
		btnNewButton.addActionListener(new LoginButtonListener());
		contentPane.getRootPane().setDefaultButton(btnNewButton);
		panel_3.add(btnNewButton);
		
		JButton btnSerwer = new JButton("Serwer...");
		btnSerwer.addActionListener(new ServerButtonListener());
		panel_3.add(btnSerwer);
		
		pack();
	}
	
	private class LoginButtonListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			client.setUsername(loginField.getText());
			client.setPassword(passwordField.getPassword());
			LoginResponse response;
			response = client.login();
			if (response.getStatus() == 1) {
				LoginFrame.this.setVisible(false);
				// Create and display main frame
				EventQueue.invokeLater(new Runnable() {
					public void run() {
						ComunicatorFrame mainFrame = 
								new ComunicatorFrame(client);
						mainFrame.setVisible(true);
					}
				});
			} else {
				JOptionPane.showMessageDialog(LoginFrame.this,
											  response.getMessage(),
											  "Login failed",
											  JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	
	private class ServerButtonListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			String ip = (String) JOptionPane.showInputDialog(
					LoginFrame.this,
                    "Podaj IP serwera:",
                    "Ustaw serwer",
                    JOptionPane.PLAIN_MESSAGE,
                    null,
                    null,
                    client.getServerIp());
			if (ip == null) {
				return;
			}
			boolean isCorrect;
			isCorrect = ip.matches("\\d{1,3}\\.\\d{1,3}\\." +
								  "\\d{1,3}\\.\\d{1,3}");
			if(isCorrect) {
				client.setServerIp(ip);
			} else {
				JOptionPane.showMessageDialog(LoginFrame.this,
											  "Niepoprawny adres serwera.",
											  "Ustaw serwer",
											  JOptionPane.ERROR_MESSAGE);
			}
		}	
	}
}
